package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.domain.Staging;
import cn.wolfcode.crm.query.StagingQueryObject;
import cn.wolfcode.crm.service.*;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/*
* 暂存单
* */
@Controller
@RequestMapping("temporary")
public class TemporaryController {

    @Autowired
    IStagingService stagingService;

    @Autowired
    ICorrectingService correctingService;

    @Autowired
    IAuditService auditService;


    @Autowired
    IHistoryService historyService;

    @Autowired
    IProductService productService;


    @RequestMapping("viewTemporary")
    @RequiresPermissions(value = {"temporary:viewTemporary", "暂存单页面"}, logical = Logical.OR)
    public String viewTemporary() {
        return "temporary";
    }

    @RequestMapping("queryTemporary")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:queryTemporary", "暂存单列表"}, logical = Logical.OR)
    public PageResult queryTemporary(StagingQueryObject qo) {
        return stagingService.query(qo);
    }


    @RequestMapping("drop")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:reset", "拒保"}, logical = Logical.OR)
    public JsonResult reset(Long id){
        try{
            stagingService.drop(id);

        }catch(Exception e1){
            e1.printStackTrace();
            return JsonResult.mark(e1.getMessage());
        }
        return JsonResult.success("拒保成功");

    }

    @RequestMapping("remand")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:remand", "提审"}, logical = Logical.OR)
    public JsonResult remand(Long id){
        try{
            stagingService.remand(id);

        }catch(Exception e1){
            e1.printStackTrace();
            return JsonResult.mark(e1.getMessage());
        }
        return JsonResult.success("提审成功");

    }
    @RequestMapping("back")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:back", "退回申请"}, logical = Logical.OR)
    public JsonResult back(Long id,String opinion){
        try{
            stagingService.back(id,opinion);

        }catch(Exception e1){
            e1.printStackTrace();
            return JsonResult.mark(e1.getMessage());
        }
        return JsonResult.success("退回成功");

    }
    @RequestMapping("through")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:through", "审核通过"}, logical = Logical.OR)
    public JsonResult through(Long id,Staging record){
        try{
            stagingService.through(id,record);


        }catch(Exception e1){
            e1.printStackTrace();
            return JsonResult.mark(e1.getMessage());
        }
        return JsonResult.success("审核通过成功");

    }

    @RequestMapping("viewAmend")
    @RequiresPermissions(value = {"temporary:viewAmend", "批改单页面"}, logical = Logical.OR)
    public String viewAmend() {
        return "correcting";
    }

    @RequestMapping("queryAmend")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:queryAmend", "批改单列表"}, logical = Logical.OR)
    public PageResult queryAmend(StagingQueryObject qo) {
        return correctingService.query(qo);
    }




    @RequestMapping("viewAudit")
    @RequiresPermissions(value = {"temporary:viewAudit", "待审核单页面"}, logical = Logical.OR)
    public String viewAudit() {
        return "audit";
    }

    @RequestMapping("queryAudit")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:queryAudit", "待审核单列表"}, logical = Logical.OR)
    public PageResult queryAudit(StagingQueryObject qo) {
        return auditService.query(qo);
    }


    @RequestMapping("viewRecord")
    @RequiresPermissions(value = {"temporary:viewRecord", "单据历史记录页面"}, logical = Logical.OR)
    public String viewRecord() {
        return "history";
    }

    @RequestMapping("queryRecord")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:queryRecord", "单据历史记录列表"}, logical = Logical.OR)
    public PageResult queryRecord(StagingQueryObject qo) {
        return historyService.query(qo);
    }




    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:saveOrUpdate","车险投保新增"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Staging staging,Long[] pIds){
        try{
            if(staging.getId()!=null){
                stagingService.updateByPrimaryKey(staging);
            }else{
                stagingService.insert(staging,pIds);
            }
            return JsonResult.success("更新成功");
        }catch(Exception e1){
            e1.printStackTrace();
            return JsonResult.mark(e1.getMessage());
        }

    }

    @RequestMapping("selectByPrimaryKey")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:selectByPrimaryKey","查订单产品信息"},logical = Logical.OR)
    public List<Product> selectByPrimaryKey(Long id){
        List<Product> list = new ArrayList<>();
        list.add(productService.selectByPrimaryKey(id));
        return list;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:selectAll","查订单产品信息"},logical = Logical.OR)
    public List<Product> selectAll(){
        return productService.selectAll();
    }

    @RequestMapping("echo")
    @ResponseBody
    @RequiresPermissions(value = {"temporary:echo","编辑回显数据"},logical = Logical.OR)
    public List<Staging> echo(Long id){
        List<Staging> list = new ArrayList<>();
        list.add(stagingService.echo(id));
        return list;
    }


}

